﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SimpleTerminal.Common;
using SQLite;

namespace SimpleTerminal
{
    public partial class KeywordForm : Form
    {
        private bool EditMode = false;
        private Keyword keyword;
        public KeywordForm()
        {
            InitializeComponent();
        }

        public KeywordForm(int id) : this()
        {
            if (id != 0)
            {
                using (Datastore datastore = new Datastore())
                {
              //      Console.WriteLine(id);
                    keyword = datastore.Keyword(id);
                    EditMode = true;
                    KeyWordTextBox.Text = keyword.Pattern;
                    CaseSensitiveCheckBox.Checked = keyword.CaseSensitive;
                    RegularCheckBox.Checked = keyword.Regular;
                    ExplainTextBox.Text = keyword.Explain;
                    SchemeCB.SelectedIndex = keyword.Scheme;
                }

            }
        }

        private void TextBox1_Validating(object sender, CancelEventArgs e)
        {
            if (KeyWordTextBox.Text.Trim().Length == 0)
            {
                e.Cancel = true;
                errorProvider1.SetError(sender as Control, "关键词不能为空");
            }
            else
            {
                errorProvider1.SetError(sender as Control, null);
            }
        }

        private void TextBox2_Validating(object sender, CancelEventArgs e)
        {
            if (ExplainTextBox.Text.Trim().Length == 0)
            {
                e.Cancel = true;
                errorProvider1.SetError(sender as Control, "说明不能为空");
            }
            else
            {
                errorProvider1.SetError(sender as Control, null);
            }
        }

        private void SaveBtn_Click(object sender, EventArgs e)
        {
            using (Datastore datastore = new Datastore())
            {
                Keyword keyword2 = new Keyword
                {

                    
                    Pattern = KeyWordTextBox.Text,
                    CaseSensitive = CaseSensitiveCheckBox.Checked,
                    Regular = RegularCheckBox.Checked,
                    Explain = ExplainTextBox.Text,
                    Scheme = SchemeCB.SelectedIndex
                };
                if (EditMode)
                {
                    keyword2.Enable = keyword.Enable;
                    keyword2.Id = keyword.Id;
                    keyword2.SortBy = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds;

                    datastore.UpdateKeyword(keyword2);

                }
                else
                {
                    keyword2.SortBy = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds;
                    datastore.InsetKeyword(keyword2);

                }
            }

            this.DialogResult = DialogResult.OK;
            Close();


        }
    }
}
